/**
 * 
 */
package com.appspot.android2gather.server;

import static com.appspot.android2gather.activity.RequestParameters.assigned;

import java.io.IOException;
import java.util.HashSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

/**
 * @author stefan
 */
public class LogonManager extends HttpServlet {
	/**
     * 
     */
	private static final long serialVersionUID = 91081175439816423L;

	/**
	 * Login
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		/*
		 * Allows to control HTTP method from browser
		 */
		if (assigned(req.getParameter("m"))) {
			if (req.getParameter("m").equals("post")) {
				doPost(req, resp);
				return;
			}
			if (req.getParameter("m").equals("put")) {
				doPut(req, resp);
				return;
			}
			if (req.getParameter("m").equals("delete")) {
				doDelete(req, resp);
				return;
			}
		}
		// check logon
		if (assigned(req.getParameter("check"))) {
			if (req.getUserPrincipal() != null) {
				resp.getWriter().print("true");
				return;
			} else {
				resp.getWriter().print("false");
				return;
			}
		}

		// else scho login
		if (req.getUserPrincipal() != null) {
			UserService userService = UserServiceFactory.getUserService();
			String destinationURL = req.getRequestURL().toString();

			resp.getWriter().print(
					"Successfully logged on as "
							+ req.getUserPrincipal().getName() + " ");
			resp.getWriter().print(
					"<a href=" + userService.createLogoutURL(destinationURL)
							+ ">Logout</a>");
			return;
		}

		String destinationURL = req.getRequestURL().toString();

		printLoginProvider(resp, "images/googleB.png", destinationURL, "",
				"gmail.com");
		resp.getWriter().println("<br/>");
		printLoginProvider(resp, "images/myopenidB.png", destinationURL, "",
				"myopenid.com");
		resp.getWriter().println("<br/>");
		printLoginProvider(resp, "images/flickrB.png", destinationURL, "",
				"flickr.com");
		resp.getWriter().println("<br/>");
		printLoginProvider(resp, "images/facebookB.png", destinationURL, "",
				"facebook.com/oauth");
		resp.getWriter().println("<br/>");
		printLoginProvider(resp, "images/yahooB.png", destinationURL, "",
				"yahoo.com");
		resp.getWriter().println("<br/>");
		printLoginProvider(resp, "images/aolB.png", destinationURL, "",
				"aol.com");
		resp.getWriter().println("<br/>");
		printLoginProvider(resp, "images/myspaceB.png", destinationURL, "",
				"myspace.com");
	}

	/**
	 * Creates an image with a link for an OpenId login provider
	 * 
	 * @param response
	 * @param logoURL
	 * @param destinationURL
	 * @param domain
	 * @param federateProviderId
	 * @throws IOException
	 */
	private void printLoginProvider(HttpServletResponse response,
			String logoURL, String destinationURL, String domain,
			String federateProviderId) throws IOException {

		response.getWriter().println(
				"<a style=\"text-decoration:none\" href=\""
						+ UserServiceFactory.getUserService().createLoginURL(
								destinationURL, domain, federateProviderId,
								new HashSet<String>()) + "\"><img alt=\""
						+ federateProviderId + " logo\" border=\"0\" src=\""
						+ logoURL + "\" class=\"openIdProviderLogo\" /></a>");

	}

	@Override
	protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		UserService userService = UserServiceFactory.getUserService();
		String destinationURL = req.getRequestURL().toString();

		if (req.getUserPrincipal() != null) {
			resp.sendRedirect(userService.createLogoutURL(destinationURL));
		}

	}
}
